package com.example.image.loader

import android.annotation.SuppressLint
import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.example.image.IImageLoader
import com.example.image.ImageOptions
import java.io.File
import java.lang.ref.SoftReference

/**
 * UniversalImageLoader图片加载器
 */
class PicassoImageLoader:IImageLoader {
    override fun loadFileIntoImageView(context: Context, file: File, target: ImageView, options: ImageOptions) {
        loadImage(context,file,target,options)
    }
    override fun loadUrlIntoImageView(context: Context, url: String, target: ImageView, options: ImageOptions) {
        loadImage(context,url,target,options)
    }
    override fun loadResourceIntoImageView(context: Context, resourceId: Int, target: ImageView, options: ImageOptions) {
        loadImage(context,resourceId,target,options)
    }

    override fun loadByteArrayIntoImageView(context: Context, bytes: ByteArray, target: ImageView, options: ImageOptions) {
        loadImage(context,bytes,target,options)
    }

    override fun loadUriIntoImageView(context: Context, uri: Uri, target: ImageView, options: ImageOptions) {
        super.loadUriIntoImageView(context, uri, target, options)
    }

    @SuppressLint("CheckResult")
    private fun loadImage(context: Context, source: Any, target:ImageView, options: ImageOptions){
        val softReference = SoftReference(context)
        softReference.get()?.run {
            val requestManager= Glide.with(this)
            //是否加载gif图
            if(options.isGifImage){
                requestManager.asGif()
            }
            //获取请求Builder
            var requestBuilder = requestManager.load(source)
            //加载占位图
            if(options.placeImage>0){
                requestBuilder.placeholder(options.placeImage)
            }
            //加载错误占位图
            if(options.errorImage>0){
                requestBuilder.error(options.errorImage)
            }
            //是否使用内存缓存
            if(options.skipMemoryCache){
                requestBuilder.skipMemoryCache(true)
            }
            //是否使用磁盘缓存
            if(options.skipDiskCache){
                requestBuilder.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            }else{
                requestBuilder.diskCacheStrategy(DiskCacheStrategy.NONE)
            }
            //设置图片固定大小
            if(options.imgSize.size == 2){
                requestBuilder.submit(options.imgSize[0],options.imgSize[1])
            }
            //设置圆形图标
            if(options.isCircle){
                requestBuilder.transform(CenterCrop(), CircleCrop())
            }else{
                requestBuilder.transform(CenterCrop())
            }
            //设置圆角
            if(options.roundRadius>0){
                requestBuilder.transform(CenterCrop(), RoundedCorners(options.roundRadius))
            }


            requestBuilder.into(target)
        }

        val requestBuilder = Glide.with(context).load(source)

        requestBuilder.placeholder(options.placeImage)
    }
}